package kiwi.assist.dev.guieditor.mediator {
	import ghostcat.display.other.GContextMenu;
	import ghostcat.util.display.DisplayUtil;

	import kiwi.assist.dev.guieditor.event.GUIEditorEvent;
	import kiwi.assist.dev.guieditor.event.GUIOperateEvent;
	import kiwi.assist.dev.guieditor.model.ContainerConfig;
	import kiwi.assist.dev.guieditor.model.GUIEditorConfig;
	import kiwi.assist.dev.guieditor.model.SkinConfig;
	import kiwi.assist.dev.guieditor.model.StyleLibConfig;
	import kiwi.assist.dev.guieditor.model.UiDetailVO;
	import kiwi.assist.dev.guieditor.view.GuiEditorView;
	import kiwi.assist.dev.util.vectoryToArray;
	import kiwi.util.MapUtils;

	import com.demonsters.debugger.MonsterDebugger;

	import org.aswing.JTree;
	import org.aswing.event.InteractiveEvent;
	import org.aswing.event.TreeSelectionEvent;
	import org.aswing.tree.DefaultMutableTreeNode;
	import org.aswing.tree.DefaultTreeModel;
	import org.aswing.tree.TreePath;
	import org.robotlegs.mvcs.Mediator;

	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * @author zhangming.luo
	 */
	public class GuiEditorMediator extends Mediator {
		[Inject]
		public var config : GUIEditorConfig;
		[Inject]
		public var view : GuiEditorView;
		[Inject]
		public var detail : UiDetailVO;

		public function GuiEditorMediator() {
		}

		override public function onRegister() : void {
			addContextListener(GUIEditorEvent.CONFIG_CHANGE, handleConfigChange);
			addContextListener(GUIEditorEvent.STYLE_CHANGE, handleStyleChange);
			addContextListener(GUIEditorEvent.STYLE_SELECTION_CHANGE, handleStyleSelected);
			addContextListener(GUIEditorEvent.CONTAINER_CHANGE, handleContainerChange);
			addContextListener(GUIOperateEvent.BACKGROUND_ADD, handleBackgroundAdd);
			addContextListener(GUIOperateEvent.UI_ADD, handleUiAdd);
			addContextListener(GUIOperateEvent.UI_REMOVE, handleUiRemove);
			addContextListener(GUIOperateEvent.BACKGROUND_REMOVE, handleBackgroundRemove);
			eventMap.mapListener(view.styleCombo, InteractiveEvent.SELECTION_CHANGED, handleStyleSelectionChange);
			eventMap.mapListener(view.backgroundLayerCheckbox, InteractiveEvent.SELECTION_CHANGED, handleLayerChange);
			eventMap.mapListener(view.componentLayerCheckbox, InteractiveEvent.SELECTION_CHANGED, handleLayerChange);
			eventMap.mapListener(view.backgroundLayerLockCheckbox, InteractiveEvent.SELECTION_CHANGED, handleLayerLockChange);
			eventMap.mapListener(view.componentLayerLockCheckbox, InteractiveEvent.SELECTION_CHANGED, handleLayerLockChange);
			eventMap.mapListener(view.skinTree, TreeSelectionEvent.TREE_SELECTION_CHANGED, handleTreeSelectionChange);
			eventMap.mapListener(view.addBackgroundButton, MouseEvent.CLICK, handleAddBackground);
			bindMenu();
			dispatch(new GUIEditorEvent(GUIEditorEvent.VIEW_COMPLETE, view));
		}

		private function handleUiRemove(e : GUIOperateEvent) : void {
			view.contentCanvas.removeChild(e.targetUi.parent);
		}

		private function handleBackgroundRemove(e : GUIOperateEvent) : void {
			view.backgroundCanvas.removeChild(e.targetUi.parent);
		}

		private function handleUiAdd(e : GUIOperateEvent) : void {
			var sprite : Sprite = new Sprite();
			view.contentCanvas.addChild(sprite);
			// 这个顺序千万不能变
			sprite.addChild(e.targetUi);
		}

		private function handleBackgroundAdd(e : GUIOperateEvent) : void {
			var sprite : Sprite = new Sprite();
			view.backgroundCanvas.addChild(sprite);
			// 这个顺序千万不能变
			sprite.addChild(e.targetUi);
		}

		private function handleAddBackground(e : MouseEvent) : void {
			dispatch(new GUIEditorEvent(GUIEditorEvent.SHOW_BACKGROUND_SELECT_DIALOG));
		}

		private function handleContainerChange(e : GUIEditorEvent) : void {
			DisplayUtil.removeAllChildren(view.containerCanvas);
			var containerConfig : ContainerConfig = ContainerConfig(config.getUiConfigByDos(e.data));
			view.contentCanvas.x = view.backgroundCanvas.x = containerConfig.leftPadding;
			view.contentCanvas.y = view.backgroundCanvas.y = containerConfig.topPadding;
			var sprite : Sprite = new Sprite();
			view.containerCanvas.addChild(sprite);
			sprite.addChild(e.data);
		}

		private function bindMenu() : void {
			// 自定义的菜单
			var menu : GContextMenu = new GContextMenu();
			menu.addMenu("MonsterDebug", function() : void {
				MonsterDebugger.initialize(view.stage);
			});
			menu.parse(view);
			view.backgroundLayerCheckbox.setSelected(true);
			view.componentLayerCheckbox.setSelected(true);
		}

		private function handleTreeSelectionChange(e : TreeSelectionEvent) : void {
			var path : TreePath = e.getPath();
			var skinConfig : SkinConfig = DefaultMutableTreeNode(path.getLastPathComponent()).getUserObject() as SkinConfig;
			dispatch(new GUIEditorEvent(GUIEditorEvent.SKIN_SELECTION_CHANGE, skinConfig));
		}

		private function handleLayerLockChange(e : InteractiveEvent) : void {
			var target : Sprite;
			switch(e.target) {
				case view.backgroundLayerLockCheckbox:
					target = view.backgroundCanvas;
					target.mouseChildren = !view.backgroundLayerLockCheckbox.isSelected();
					target.mouseEnabled = !view.backgroundLayerLockCheckbox.isSelected();
					break;
				case view.componentLayerLockCheckbox:
					target = view.contentCanvas;
					target.mouseChildren = !view.componentLayerLockCheckbox.isSelected();
					target.mouseEnabled = !view.componentLayerLockCheckbox.isSelected();
					break;
			}
			dispatch(new GUIEditorEvent(GUIEditorEvent.LAYER_CHANGE, e.target));
		}

		private function handleLayerChange(e : InteractiveEvent) : void {
			var target : Sprite;
			switch(e.target) {
				case view.backgroundLayerCheckbox:
					target = view.backgroundCanvas;
					target.visible = view.backgroundLayerCheckbox.isSelected();
					break;
				case view.componentLayerCheckbox:
					target = view.contentCanvas;
					target.visible = view.componentLayerCheckbox.isSelected();
					break;
			}
			dispatch(new GUIEditorEvent(GUIEditorEvent.LAYER_CHANGE, e.target));
		}

		private function handleStyleChange(e : GUIEditorEvent) : void {
			// 更新列表
			var tree : JTree = view.skinTree;
			var root : DefaultMutableTreeNode = new DefaultMutableTreeNode("资源");
			var treeModel : DefaultTreeModel = new DefaultTreeModel(root);
			for each (var style:StyleLibConfig in config.styles) {
				var styleRoot : DefaultMutableTreeNode = new DefaultMutableTreeNode(style.name);
				for each (var skinName:String in MapUtils.getMapKeyArray(style.skines).sort()) {
					var node : DefaultMutableTreeNode = new DefaultMutableTreeNode(style.skines[skinName], false);
					styleRoot.append(node);
				}
				root.append(styleRoot);
			}
			tree.setModel(treeModel);
		}

		private function handleStyleSelectionChange(e : InteractiveEvent) : void {
			dispatch(new GUIEditorEvent(GUIEditorEvent.STYLE_SELECTION_CHANGE, view.styleCombo.getSelectedItem()));
		}

		private function handleStyleSelected(e : GUIEditorEvent) : void {
			var item : StyleLibConfig = e.data;
			view.styleCombo.setListData(vectoryToArray(config.styles));
			if (config.styles.indexOf(item) >= 0 ) {
				view.styleCombo.setSelectedItem(item);
			}
		}

		private function handleConfigChange(e : GUIEditorEvent) : void {
			// 更新style列表
			var item : * = view.styleCombo.getSelectedItem();
			view.styleCombo.setListData(vectoryToArray(config.styles));
			if (config.styles.indexOf(item) >= 0 ) {
				view.styleCombo.setSelectedItem(item);
			}
		}
	}
}
